/**
 * For the license, see main.cpp file
 */

#include "CronTimer.h"
#include "Qrm3Controller.h"

CronTimer::CronTimer(quint32 minuteInterval) 
{
	timer = new QTimer(this);
	timer->setInterval(minuteInterval * 1000 * 60);
	
	connect(timer, SIGNAL(timeout()), this, SLOT(doOnTimeout()));
	
}

CronTimer::~CronTimer() { }

void CronTimer::doOnTimeout()
{
	qDebug() << "timeout.";
	Qrm3Controller::getInstance()->getAuthenticator()->start();
}

void CronTimer::startCron()
{
	qDebug() << "cron started";
	
	Qrm3Controller::getInstance()->getAuthenticator()->start();
	
	timer->start();
	
	emit cronStarted();
}

void CronTimer::stopCron()
{
	qDebug() << "cron stopped";
	timer->stop();
	emit cronStopped();
}


void CronTimer::changeInterval(quint32 newMinuteInterval)
{
	if(timer->isActive())
	{
		/* neet a stop and restart */
		timer->stop();
		Qrm3Controller::getInstance()->getTimeLine()->stop();
		Qrm3Controller::getInstance()->getGui()->ui.CronProgressBar->setValue(0);
		emit cronStopped();
		
		timer->setInterval(newMinuteInterval * 1000 * 60);
		Qrm3Controller::getInstance()->getTimeLine()->setDuration(newMinuteInterval * 1000 * 60);
		
		timer->start();
		Qrm3Controller::getInstance()->getTimeLine()->setCurrentTime(0);
		Qrm3Controller::getInstance()->getTimeLine()->start();
		emit cronStarted();
	}
	else
	{
		timer->setInterval(newMinuteInterval * 1000 * 60);
		Qrm3Controller::getInstance()->getTimeLine()->setDuration(newMinuteInterval * 1000 * 60);
	}
}

QTimer* CronTimer::getTimer() { return timer; }
